package com.wy.framework.actions.role;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.itextpdf.text.log.SysoCounter;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.constants.RtnCodeConstant;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.pojos.TSysOrgroles;
import com.wy.framework.pojos.TSysRoleinfo;
import com.wy.framework.services.permission.RoleManageService;

/**
 * 项目名称：openportal
 * 类名称：RoleAction
 * 类描述：角色信息管理
 * 创建人：张旭星
 * 创建时间：2015-12-28 下午9:41:44
 * 修改人：张旭星
 * 修改时间：2015-12-28 下午9:41:44
 * 版本信息：@version 1.0
 * Copyright (c) 2015万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@ParentPackage("json")
@Namespace("/manage/role")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class RoleAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(RoleAction.class);

	@Autowired
	private RoleManageService roleManageService;
	
	protected TSysRoleinfo role;
	protected TSysOrgroles orgRole;
	
	private List<Map> rows;
	private List<Map> roleInfo;

	@Action(value = "query", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String query() throws ServiceException {
		try {
			String orgCode = ServletActionContext.getRequest().getParameter("orgCode");
			String searchContent=ServletActionContext.getRequest().getParameter("searchContent");
			rows = roleManageService.queryRoleInfo(orgCode,searchContent);
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	@Action(value = "loadRoleInfoForUser", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","roleInfo" }) })
	public String loadRoleInfoForUser() throws ServiceException {
		String orgCode=ServletActionContext.getRequest().getParameter("orgCode");
		roleInfo = roleManageService.loadRoleInfoForUser(orgCode);
		return SUCCESS;
	}
	
	@Action(value = "loadUserByRoleId", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String loadUserByRoleId() throws ServiceException {
		String roleId=ServletActionContext.getRequest().getParameter("roleId");
		roleInfo = roleManageService.loadUserByRoleId(roleId);
		return SUCCESS;
	}
	
	@Action(value = "add", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String add()throws ServiceException{
		rspCode=roleManageService.saveRoleInfo(role, orgRole);
		return SUCCESS;
	}
	
	@Action(value = "delete", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String delete()throws ServiceException{
		 String roleId=ServletActionContext.getRequest().getParameter("roleId");
		 rspCode=roleManageService.deleteRoleInfo(roleId);
		 return SUCCESS;
	}
	
	@Action(value = "modify", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json",  params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String modify()throws ServiceException{
		 rspCode=roleManageService.updateRoleInfo(role);
		 return "success";
	}
	
	public List<Map> getRows() {
		return rows;
	}

	public List<Map> getRoleInfo() {
		return roleInfo;
	}

	public int getTotal() {
		if (rows == null) {
			return 0;
		} else {
			return rows.size();
		}
	}

	public TSysRoleinfo getRole() {
		return role;
	}

	public void setRole(TSysRoleinfo role) {
		this.role = role;
	}

	public TSysOrgroles getOrgRole() {
		return orgRole;
	}

	public void setOrgRole(TSysOrgroles orgRole) {
		this.orgRole = orgRole;
	}
	
	

}
